/***********************************************************************
	filename: 	4YsXMLParser.cpp
	created:	18/4/2012
	author:		4Ys
	
	purpose:	Implements classes of XMLParser with TinyXML.	            
*************************************************************************/

#include "4YsXMLParser.h"


namespace Dater
{
	//----------------------------------------------------------------------------//
	XMLParser::XMLParser()
	{
		_appPath = "../";
		_myXml = new TiXmlDocument();
	}
	
	//----------------------------------------------------------------------------//
	XMLParser::~XMLParser()
	{
		delete _myXml;
	}

	//----------------------------------------------------------------------------//
	bool XMLParser::createXmlFile()
	{
		try
		{
			std::string fullFilePath = _appPath + _xmlName;
			if(!_myXml->LoadFile(fullFilePath.c_str()))
			{
				//create a root element and link to it.
				TiXmlElement* rootElement = new TiXmlElement(_xmlName);
				_myXml->LinkEndChild(rootElement);
			}
		}

		catch(std::string& e)
		{
			return false;
		}
		return true;
	}
		
	//----------------------------------------------------------------------------//
	void XMLParser::dropXmlFile()
	{
		try
		{
			_myXml->Clear();
		}

		catch(std::string& e)
		{
			
		}
	}
	//----------------------------------------------------------------------------//
	void XMLParser::split(std::string const& order, std::string delimiter, std::vector<std::string>& splitedVector)
	{
		splitedVector.clear();
		size_t pos = 0, found = 0;
		while(found != std::string::npos)
		{
			found = order.find(delimiter, pos);
			splitedVector.push_back(std::string(order, pos, found - pos));
			pos = found + 1;
		}
		return;
	}

	//----------------------------------------------------------------------------//
	void XMLParser::createElement(TiXmlElement* element, std::string elementname, std::string content)
	{
		TiXmlElement *anElement = new TiXmlElement(elementname);
		element->LinkEndChild(anElement);
		TiXmlText *aContent = new TiXmlText(content);
		anElement->LinkEndChild(aContent);
		return;
	}

	//----------------------------------------------------------------------------//
}